import 'dart:math';

import 'package:flutter/widgets.dart';

extension ScrollControllerEx on ScrollController {
  void scrollToBottom({
    bool isAnimate = false,
  }) {
    if (isAnimate) {
      animateTo(position.extentAfter,
          duration: const Duration(milliseconds: 80), curve: Curves.easeInOut);
    } else {
      jumpTo(position.extentAfter);
    }
  }

  void scrollToTop({
    bool isAnimate = false,
  }) {
    if (isAnimate) {
      animateTo(0,
          duration: const Duration(milliseconds: 80), curve: Curves.easeInOut);
    } else {
      jumpTo(0);
    }
  }

  void scrollTo(
    double x, {
    bool isAnimate = false,
  }) {
    var off = min(max(0.0, x), position.maxScrollExtent);
    if (isAnimate) {
      animateTo(off,
          duration: const Duration(milliseconds: 80), curve: Curves.easeInOut);
    } else {
      jumpTo(off);
    }
  }
}
